package everydayGrade.t1;

import java.util.*;

/**
 * @Author: Siantar
 * @Date: 2023-12-12-17:31
 * @Description: 1.0
 */
public class T2454 {
    public int[] secondGreaterElement(int[] nums) {
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            while (!stack2.isEmpty() && nums[stack2.peek()] < nums[i]) {
                nums[stack2.pop()] = nums[i];
            }
            Stack<Integer> stackT = new Stack<>();
            while (!stack1.isEmpty() && nums[stack1.peek()] < nums[i]) {
                stackT.push(stack1.pop());
            }
            while (!stackT.isEmpty()) {
                stack2.push(stackT.pop());
            }
            stack1.push(i);
        }
        while (!stack1.isEmpty()) nums[stack1.pop()] = -1;
        while (!stack2.isEmpty()) nums[stack2.pop()] = -1;
        return nums;
    }
}
